package recallin.activities;

import recallin.database.ServerConnection;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Handles the login of the whole application 
 * and also saves "cookies" for the pass and username
 * @author philipmasek
 *
 */

public class LoginActivity extends Activity {

	private ImageView logo;
	private EditText username;
	private EditText password;
	private Button login;
	private ProgressDialog progressDialog;
	private Handler h = new Handler();
	
	public static final String PREFS_NAME = "MyPrefsFile";
	public static final String PREF_USERNAME = "username";
	public static final String PREF_PASSWORD = "password";
	
	
	public static ServerConnection connection = new ServerConnection();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   
		final String username = pref.getString(PREF_USERNAME, null);
		final String password = pref.getString(PREF_PASSWORD, null);
		if (username==null||password==null) {
			init();
		}else{
			progressDialog = ProgressDialog.show(LoginActivity.this, "", "Loading");
			Thread thread = new Thread(){
				@Override
				public void run() {
					Looper.prepare();
					String log = connection.login(username, password);
					progressDialog.dismiss();
					Intent intent = new Intent(LoginActivity.this,
							MainActivity.class);
					Toast.makeText(LoginActivity.this, "Welcome to "+log+" mode!",
							Toast.LENGTH_LONG).show();
					startActivity(intent);
					super.run();
				}
			};
			thread.start();
		}
		
	}

	private void init() {
		setContentView(R.layout.login_layout);

		logo = (ImageView) findViewById(R.id.login_logo);
		username = (EditText) findViewById(R.id.login_usr);
		password = (EditText) findViewById(R.id.login_pwr);
		login = (Button) findViewById(R.id.login_button);

		logo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);
				Toast.makeText(LoginActivity.this, "Welcome to offline mode!",
						Toast.LENGTH_LONG).show();
				startActivity(intent);
			}
		});

		login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				progressDialog = ProgressDialog.show(LoginActivity.this, "", "Loading");
				Thread thread = new Thread(){
					@Override
					public void run() {
						Looper.prepare();
						String log = connection.login(username.getText().toString(), password.getText().toString());
						progressDialog.dismiss();
						if (log.equalsIgnoreCase("User not found."))
							h.postDelayed(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(LoginActivity.this, "User not found.", Toast.LENGTH_LONG).show();
								}
							}, 100);
						else{
							getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
					        .edit()
					        .putString(PREF_USERNAME, username.getText().toString())
					        .putString(PREF_PASSWORD, password.getText().toString())
					        .commit();
							Intent intent = new Intent(LoginActivity.this,
									MainActivity.class);
							Toast.makeText(LoginActivity.this, "Welcome to "+log+" mode!",
									Toast.LENGTH_LONG).show();
							startActivity(intent);
						}
						super.run();
					}
				};
				thread.start();
			}
		});

	}

	@Override
	public void onBackPressed() {
	}
	
}
